package com.unknowpupil.alibaba.cloud.oauth.controller;


import com.unknowpupil.alibaba.cloud.dto.ErrorState;
import com.unknowpupil.alibaba.cloud.dto.ResponseResult;
import com.unknowpupil.alibaba.cloud.oauth.vo.LoginPwdDto;
import com.unknowpupil.alibaba.cloud.utils.JsonUtils;
import com.unknowpupil.alibaba.cloud.utils.OkHttpClientUtil;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: <br/>
 * <p>
 *
 * </p>
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/5/16 22:03
 * @Classname: Oauth2Controller
 * @Version: 1.0
 */
@Controller
public class Oauth2Controller {

    private static final String URL_OAUTH_TOKEN = "http://localhost:8083/oauth/token";
    @Value("${consumer.oauth2.grantType}")
    private String oauth2GrantType;
    @Value("${consumer.oauth2.clientId}")
    private String oauth2ClientId;
    @Value("${consumer.oauth2.clientSecret}")
    private String oauth2ClientSecret;

    @RequestMapping("oauth/login")
    @ResponseBody
    @ResponseStatus(code = HttpStatus.UNAUTHORIZED)
    public ResponseResult requireAuthentication(@RequestBody LoginPwdDto dto) throws IOException {
        Map<String, String> param = new HashMap(5);
        param.put("username", dto.getUsername());
        param.put("password", dto.getPassword());
        param.put("grant_type", oauth2GrantType);
        param.put("client_id", oauth2ClientId);
        param.put("client_secret", oauth2ClientSecret);
        try {
            Response response = (Response) OkHttpClientUtil.getInstance().postData(URL_OAUTH_TOKEN, param);
            String jsonString = Objects.requireNonNull(response.body()).string();
            Map<String, Object> jsonMap = JsonUtils.json2map(jsonString);
            if (response.code() != ErrorState.SUCCESS.getCode()) {
                return ResponseResult.buildFailure(response.code(), response.message());
            }
            return ResponseResult.buildSuccess(jsonMap);
        } catch (Exception e) {
            return ResponseResult.buildFailure("认证服服务器异常");
        }

    }
    @RequestMapping("oauth/login/sms")
    @ResponseBody
    @ResponseStatus(code = HttpStatus.UNAUTHORIZED)
    public ResponseResult sms(@RequestBody LoginPwdDto dto) throws IOException {
        Map<String, String> param = new HashMap(5);
        param.put("username", dto.getUsername());
        param.put("password", dto.getPassword());
        param.put("grant_type", oauth2GrantType);
        param.put("client_id", oauth2ClientId);
        param.put("client_secret", oauth2ClientSecret);
        param.put("auth_type", "sms");
        try {
            Response response = (Response) OkHttpClientUtil.getInstance().postData(URL_OAUTH_TOKEN, param);
            String jsonString = Objects.requireNonNull(response.body()).string();
            Map<String, Object> jsonMap = JsonUtils.json2map(jsonString);
            if (response.code() != ErrorState.SUCCESS.getCode()) {
                return ResponseResult.buildFailure(response.code(), response.message());
            }
            return ResponseResult.buildSuccess(jsonMap);
        } catch (Exception e) {
            return ResponseResult.buildFailure("认证服服务器异常");
        }

    }

    @ResponseBody
    @RequestMapping("ologin")
    public String oauthLogin() {
        return "oauthLogin";
    }

    //授权控制器
    @ResponseBody
    @RequestMapping("user/info")
    public String getAccessConfirmation(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "user";
    }
}
